package screens;

import com.alee.laf.panel.WebPanel;
import model.UserModel;
import sql.Db;
import sql.ResultSetHandler;
import util.FontLoader;
import view.PostList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.sql.ResultSet;

public class ProfileScreen extends WebPanel {

    private PostList postList;
    private UserModel user;

    /**
     * Show the posts for this targetID
     * @param targetId TargetID to show posts for, can be an eventID or a userID.
     */
    public ProfileScreen(String targetId) {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setOpaque(false);

        showInfo(targetId);
        showPosts(targetId);
        postList.inputLabel.setText("Share something with " + user.name + "!");

    }

    public void showPosts(String targetId) {

        postList = new PostList(targetId);
        this.add(postList);
    }

    public void showInfo(String targetId) {
        Db.getDb().executeQuery("SELECT *\n" +
                "FROM user\n" +
                "WHERE id = " + targetId + "\n",

                new ResultSetHandler() {
                    @Override
                    public void handleResult(ResultSet rs) {
                        try {
                            if (rs.next()) {
                                UserModel um = user = new UserModel(rs);

                                Font f = FontLoader.getFont(FontLoader.OPENSANS_BOLD);
                                JLabel t = new JLabel(um.name);
                                t.setForeground(Color.GRAY);
                                t.setAlignmentX(Component.CENTER_ALIGNMENT);
                                f = f.deriveFont(24f);
                                t.setFont(f);
                                add(t);
                                add(Box.createRigidArea(new Dimension(0, 50)));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
    }

}
